package com.gupao.singleton.lazy;

//使用静态内部类来达到单例效果，tom老师评价为：史上最牛逼的单例模式
public class LazyInnerClassSingleton {

    private LazyInnerClassSingleton(){
        //防止反射入侵
        if(InnerClass.lazyInnerClassSingleton != null){
            throw new RuntimeException("不允许创建多个实例");
        }
    }

    //final是不允许方法被修改，同样是防止反射入侵
    public static final LazyInnerClassSingleton getInstance(){
        return InnerClass.lazyInnerClassSingleton;
    }

    //在使用LazyInnerClassSingleton类时就会加载内部类，不使用时不会加载内部类，可以达到节省资源
    private static class InnerClass{
        private static final LazyInnerClassSingleton lazyInnerClassSingleton = new LazyInnerClassSingleton();
    }
}
